package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.JiuZhouUserBaseData;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouSkillConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.jiuzhou.JiuZhouModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

@Cmd(code = GameProtocol.S_JIU_ZHOU_CROSS_USER_DATA_FROM_CROSS, desc = "跨服同步用户数据下来")
public class JiuZhouSyncUserDataFromCross extends AbstractCommand {

    private CrossJiuZhouProto.CrossJiuZhouCrossUserDataSyncMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossJiuZhouProto.CrossJiuZhouCrossUserDataSyncMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (player == null) {
            // 跨服下来 用户不在就不处理了
            return;
        }
        // 看下活动对不对
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null || config.getActivityId() != reqMsg.getActivityId()) {
            return;
        }
        // 更新下用户数据
        JiuZhouUserBaseData userData = player.getModule(JiuZhouModule.class).getUserData();
        if (userData == null || userData.getActivityId() != reqMsg.getActivityId()) {
            return;
        }
        userData.setZoneType(reqMsg.getZoneType());
        userData.setLastRoundId(reqMsg.getLastRoundId());
        if (reqMsg.getZoneType() == 0) {
            userData.setHasSignUp(0);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            JiuZhouProto.JiuZhouChooseSkillId2RespMsg.Builder respMsg = JiuZhouProto.JiuZhouChooseSkillId2RespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_JIU_ZHOU_CHOOSE_SKILLID2, respMsg);
        }
    }
}
